package com.jt.safe.ui.activity;

import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager2.widget.ViewPager2;

import com.jt.safe.R;
import com.jt.safe.base.BaseActivity;
import com.jt.safe.entity.CalendarBean;
import com.jt.safe.entity.DateBean;
import com.jt.safe.ui.adpter.SelectTimeAdapter;
import com.jt.safe.utils.LunarDayUtil;
import com.jt.safe.utils.SpecialDayUtil;
import com.jt.safe.viewmodel.NullViewModel;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class SelectTimeActivity extends BaseActivity<NullViewModel> {

    private List<CalendarBean> calendars = new ArrayList<>();
    private int[] startToEndMonth = {0, 7};
    private static String[] weeks = {"日", "一", "二", "三", "四", "五", "六"};
    private HashMap<String, String> priceMap = new HashMap<>();
    SelectTimeAdapter selectTimeAdapter;
    ScheduledExecutorService scheduledExecutorService;
    @Override
    protected int getLayoutId() {
            return R.layout.activity_select_time;
    }

    @Override
    public void initView() {
        initData();
        ViewPager2 viewPager2 = findViewById(R.id.view_pager);
        LinearLayout weekLayout = findViewById(R.id.week_layout);
        scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        selectTimeAdapter = new SelectTimeAdapter();
        selectTimeAdapter.setNewData(calendars);
        viewPager2.setAdapter(selectTimeAdapter);
        initPrice();

        TextView weekHint;
        weekLayout.removeAllViews();
        for (int i = 0; i < weeks.length; i++) {
            weekHint = new TextView(this);
            weekHint.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
            weekHint.setGravity(Gravity.CENTER);
            weekHint.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            weekHint.setText(weeks[i]);
            if (i == 0 || i == weeks.length - 1) {
                //周末-红色
                weekHint.setTextColor(Color.parseColor("#D83939"));
            } else {
                //工作日
                weekHint.setTextColor(Color.parseColor("#111111"));
            }
            weekLayout.addView(weekHint);
        }
    }

    @Override
    protected void createObserver() {

    }

    private void initPrice() {
        priceMap.put("2019-12-11", "360");
        priceMap.put("2019-12-12", "370");
        priceMap.put("2019-12-13", "310");
        priceMap.put("2019-12-14", "380");
        priceMap.put("2019-12-15", "250");
        priceMap.put("2019-12-16", "330");
        priceMap.put("2019-12-17", "460");
        priceMap.put("2019-12-18", "460");
        priceMap.put("2019-12-19", "470");
        priceMap.put("2019-12-20", "440");
        priceMap.put("2019-12-21", "410");
        priceMap.put("2019-12-22", "550");
        priceMap.put("2019-12-23", "250");
        priceMap.put("2019-12-24", "520");
        selectTimeAdapter.setPriceMap(priceMap);
    }

    private void initData() {
        Calendar todayCalendar = Calendar.getInstance();
        todayCalendar.setTime(new Date());
        CalendarBean calendarBean = null;
        int groupIndex = 0;
        for (int i = startToEndMonth[0]; i <= startToEndMonth[1]; i++) {
            calendarBean = new CalendarBean();
            Calendar calendarClone = (Calendar) todayCalendar.clone();
            calendarClone.add(Calendar.MONTH, i);
            calendarClone.set(Calendar.DATE, 1);
            calendarBean.setYear(calendarClone.get(Calendar.YEAR));
            calendarBean.setMonth(calendarClone.get(Calendar.MONTH) + 1);
            calendarBean.setShownTitle(calendarBean.getYear() + "-" + calendarBean.getMonth());

            // 1-星期天 7-星期六
            int dayOfWeek = calendarClone.get(Calendar.DAY_OF_WEEK);
            //上月的最后几天展示在本月
            int emptyCount = dayOfWeek - 1;
            calendarClone.roll(Calendar.DATE, -1);
            // 当月的最大天数
            int maxDays = calendarClone.get(Calendar.DATE);

            List<DateBean> daysList = new ArrayList<>();
            int maxRows = 5;
            if (emptyCount + maxDays > 35) {
                //当月有效日期+1号前空白日期个数>35,则当月需要6行
                maxRows = 6;
            }
            for (int j = 0; j < (maxRows * 7); j++) {
                DateBean dayItem = new DateBean();
                // 用于控制定位
                dayItem.setGroupIndex(groupIndex);
                dayItem.setChildIndex(j);

                Calendar calendarDayClone = (Calendar) calendarClone.clone();
                if (j < emptyCount) {
                    dayItem.setCanSelect(false);
                    //上月最后几天
                    calendarDayClone.add(Calendar.DATE, (j - emptyCount));
                    dayItem.setShownDay("");
                    dayItem.setNongliDay("");
                    daysList.add(dayItem);

                    continue;
                }
                if (j >= emptyCount + maxDays) {
                    dayItem.setCanSelect(false);
                    //下月头几天
                    calendarDayClone.add(Calendar.MONTH, 1);
                    calendarDayClone.set(Calendar.DATE, j - (emptyCount + maxDays) + 1);
                    dayItem.setShownDay("");
                    dayItem.setNongliDay("");
                    daysList.add(dayItem);

                    continue;
                }
                calendarDayClone.set(Calendar.DATE, j - emptyCount + 1);
                //显示的日期
                dayItem.setShownDay(String.valueOf(calendarDayClone.get(Calendar.DATE)));
                //显示的农历
                dayItem.setNongliDay(new LunarDayUtil(calendarDayClone).toStringSimpleDay());
                //显示的特殊日子
                dayItem.setSpecialDayTag(SpecialDayUtil.getInstance().getHolidayName(calendarDayClone));

                dayItem.setYear(calendarDayClone.get(Calendar.YEAR));
                dayItem.setMonth(calendarDayClone.get(Calendar.MONTH) + 1);
                dayItem.setDay(calendarDayClone.get(Calendar.DATE));
                dayItem.setDayOfWeek(calendarDayClone.get(Calendar.DAY_OF_WEEK));
                dayItem.setGovHoliday(SpecialDayUtil.getInstance().isGovHoliday(calendarDayClone));
                dayItem.setGovHolidayWork(SpecialDayUtil.getInstance().isGovHolidayWork(calendarDayClone));

                if (calendarDayClone.before(todayCalendar)) {
                    //今天之前
                    dayItem.setCanSelect(false);
                } else if (calendarDayClone.equals(todayCalendar)) {
                    dayItem.setShownDay("今天");
                    dayItem.setCanSelect(true);
                } else {
                    //今天之后
                    dayItem.setCanSelect(true);
                }
                dayItem.setSaverCalendar(calendarDayClone);

                daysList.add(dayItem);
            }
            calendarBean.setDateBeans(daysList);
            calendars.add(calendarBean);
            groupIndex++;
        }
    }
}
